//
//  HOMNSArrayRejectWhereTests.m
//  HigherOrderMessaging
//
//  Created by Ofri Wolfus on 10/10/06.
//  Copyright 2006 Ofri Wolfus. All rights reserved.
//

#import "HOMNSArrayRejectWhereTests.h"


@implementation HOMNSArrayRejectWhereTests

- (id)init {
	if ((self = [super init])) {
		int i;
		
		msg1 = [MSG(hasPrefix:@"a") retain];
		msg2 = [MSG(hasPrefix:@"1") retain];
		
		for (i = 0; i < 10000; i++) {
			NSString *str = [NSString stringWithFormat:@"%d", i];
			
			if (![str hasPrefix:@"1"])
				[(NSMutableArray *)largeArrayResult addObject:str];
		}
	}
	
	return self;
}

- (void)dealloc {
	[msg1 release]; msg1 = nil;
	[msg2 release]; msg2 = nil;
	[super dealloc];
}

#pragma mark -
#pragma mark Small Array Tests

- (id)testSmallArray {
	return [smallArray rejectWhere:MSG(hasPrefix:@"a"), nil];
}

- (id)testSmallArray_CachedMessage {
	return [smallArray rejectWhere:msg1, nil];
}

- (id)testSmallArray_objectAtIndex {
	unsigned i;
	NSMutableArray *r = [NSMutableArray array];
	unsigned c = [smallArray count];
	
	for (i = 0U; i < c; i++) {
		NSString *str = [smallArray objectAtIndex:i];
		
		if (![str hasPrefix:@"a"])
			[r addObject:str];
	}
	
	return r;
}

- (id)testSmallArray_NSEnumerator {
	NSMutableArray *r = [NSMutableArray array];
	NSEnumerator *e = [smallArray objectEnumerator];
	NSString *str;
	
	while ((str = [e nextObject])) {
		if (![str hasPrefix:@"a"])
			[r addObject:str];
	}
	
	return r;
}

#pragma mark -
#pragma mark Large Array Tests

- (id)testLargeArray {
	return [largeArray rejectWhere:MSG(hasPrefix:@"1"), nil];
}

- (id)testLargeArray_CachedMessage {
	return [largeArray rejectWhere:msg2, nil];
}

- (id)testLargeArray_objectAtIndex {
	unsigned i;
	NSMutableArray *r = [NSMutableArray array];
	unsigned c = [largeArray count];
	
	for (i = 0U; i < c; i++) {
		NSString *str = [largeArray objectAtIndex:i];
		
		if (![str hasPrefix:@"1"])
			[r addObject:str];
	}
	
	return r;
}

- (id)testLargeArray_NSEnumerator {
	NSMutableArray *r = [NSMutableArray array];
	NSEnumerator *e = [largeArray objectEnumerator];
	NSString *str;
	
	while ((str = [e nextObject])) {
		if (![str hasPrefix:@"1"])
			[r addObject:str];
	}
	
	return r;
}

#pragma mark -
#pragma mark Tests Verification

- (BOOL)verifyResult:(NSArray *)arr ofTest:(SEL)sel {
	NSString *name = NSStringFromSelector(sel);
	
	if ([name hasPrefix:@"testSmallArray"]) {
		DPTestAssert([arr count] == 1U, @"The result doesn't contain 1 object. Result = %@", arr);
		DPTestAssert([arr containsObject:@"b"], @"The result doesn't contain 'b'. Result = %@", arr);
	}
	
	return [super verifyResult:arr ofTest:sel];
}

@end
